package com.lt.controller;

import com.lt.pojo.BackendUser;
import com.lt.pojo.DevUser;
import com.lt.service.BackendUserService;
import com.lt.service.DevUserService;
import com.lt.utils.Constants;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    private Logger logger=Logger.getLogger(LoginController.class);

    @Autowired
    DevUserService devUserService;

    @Autowired
    BackendUserService backendUserService;




    //<editor-fold desc="后台登录模块">
    //跳转后台管理登录
    @RequestMapping("/manager/toLogin")
    public String toLoginManager(){
        return "backendlogin";
    }

    //实现后台管理登录功能
    @RequestMapping("/manager/login")
    public String loginManager(HttpSession session, HttpServletRequest request,
                               @RequestParam(value = "userCode") String userCode,
                               @RequestParam(value="userPassword") String userPassword){
        BackendUser user = null;
        try {
            user=backendUserService.loginByCodeAndPassword(userCode,userPassword);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(null!=user){
            session.setAttribute(Constants.USER_SESSION,user);
            return "redirect:/manager/toMain";
        }else {
            request.setAttribute("error","用户名或者密码不正确");
            return "backendlogin";
        }
    }

    @RequestMapping("/manager/toMain")
    public String toMain(){
        return "backend/main";
    }

    @RequestMapping("/manager/logout")
    public String managerLogout(HttpSession session){
        logger.info("managerLogout===========");
        session.removeAttribute(Constants.USER_SESSION);
        return "backendlogin";
    }
    //</editor-fold>




    //<editor-fold desc="前台登录模块">
    //跳转开发者登录
    @RequestMapping("/dev/login")
    public String toLoginDev(){
        return "devlogin";
    }

    //实现开发者登录功能
    @RequestMapping("/dev/dologin")
    public String loginDev(HttpSession session,HttpServletRequest request,
                           @RequestParam(value = "devCode") String devCode,
                           @RequestParam(value="devPassword") String devPassword){
        DevUser devUser = null;
        try {
            devUser=devUserService.loginByCodeAndPassword(devCode,devPassword);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(null!=devUser){
            session.setAttribute(Constants.DEV_USER_SESSION,devUser);
            return "redirect:/dev/toMain";
        }else {
            request.setAttribute("error","用户名或者密码不正确");
            return "devlogin";
        }
    }

    @RequestMapping("/dev/toMain")
    public String toDevMain(){
        logger.info("toDevMain===========");
        return "developer/main";
    }

    @RequestMapping("/dev/logout")
    public String devLogout(HttpSession session){
        session.removeAttribute(Constants.DEV_USER_SESSION);
        return "devlogin";
    }
    //</editor-fold>



}
